//
//  ToolImagePickerManager.swift
//  AudioToolProject
//
//  Created by 邹琳 on 2019/5/8.
//  Copyright © 2019 邹琳. All rights reserved.
//

import UIKit
import MobileCoreServices

class ToolImagePickerManager: NSObject {
    let imagePikcer:UIImagePickerController = {
        let imagePicker = UIImagePickerController()
        return imagePicker
    }()
    static let manager = ToolImagePickerManager()
    
    private var callback:((UIImage)->Void)?
    private var callbackMovie:((URL)->Void)?
    private override init() {
        super.init()
        self.imagePikcer.delegate = self
        
    }
    
    /** 相机相册选择 */
    static func alert(_ sourceType:UIImagePickerController.SourceType,finish:((UIImage)->Void)?){
        
        self.manager.alertImagePickerController(sourceType)
        self.manager.callback = finish
    }
    
    /** 分类选择资源 */
    static func alertVideos(finish:((URL)->Void)?){
        self.manager.imagePikcer.mediaTypes = [kUTTypeMovie as String]
        self.manager.callbackMovie = finish
        self.manager.alertImagePickerController(.photoLibrary)
    }
    
    private func alertImagePickerController(_ souceType:UIImagePickerController.SourceType){
        if UIImagePickerController.isSourceTypeAvailable(souceType) {
            self.imagePikcer.sourceType = souceType
            self.alertVC(self.imagePikcer)
        }else{
            print("读取相册出错")
        }
    }
    
    /** 显示提醒 */
    private func alertVC(_ controller:UIViewController){
        
        /** 判断相册是否可读 */
        if self.imagePikcer.sourceType == .camera && !AuthorizationManager.author_can_camera(){
            AlertTool.alertError("请在iPhone的”设置-隐私-相机“选项中，允许访问你的相机")
            return
        }
        if UIApplication.shared.keyWindow?.rootViewController?.presentationController != nil{
            UIApplication.shared.keyWindow?.rootViewController!.present(controller, animated: true, completion: nil)
        }else{
            UIApplication.shared.keyWindow?.rootViewController?.present(controller, animated: true, completion: nil)
        }
    }
}

extension ToolImagePickerManager:UIImagePickerControllerDelegate,UINavigationControllerDelegate{
    
    /** 取消选择 */
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        self.imagePikcer.dismiss(animated: true, completion: nil)
    }
    
    
    /** 选择完成 */
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        
        if self.callback != nil{
            guard let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage else {
                return
            }
            
            self.imagePikcer.dismiss(animated: true) {
                self.callback!(image)
            }
        }
        
        /** 如果是选择的视频 */
        if self.callbackMovie != nil{
            guard let url = info[UIImagePickerController.InfoKey.mediaURL] as? URL else{
                return
            }
            
            self.imagePikcer.dismiss(animated: true) {
                self.callbackMovie!(url)
            }
        }
    }
    
    
}
